$(function() {
    $( "#keys" ).draggable();
  });

globals = {};

globals.curTarget = {};
globals.curTarget.kom = null;
//globals.curTarget.row = 1;
//komId_4_3_1_12 = komId_komotis_sira_ipiresia_poso
globals.checkPoso = function(e, element, komId, row) {

}

globals.showStats = function(komButtonElement) {
	//alert(komButtonElement.father.onoma);
}

globals.setPoso = function(element, komId, row) {
	//var poso = element.value;
	var hiddenInput = element.father.dom.hiddenInput[row];
	var temp = hiddenInput.value;//split(",");
	temp = temp.split(",");
//alert(temp);
	temp[3] = element.father.dom.posoInput[row].value;
	hiddenInput.value = temp.join();
//console.log(temp);
	
	//hiddenInput.name = hiddenInput.name + "_" + element.value;
	hiddenInput.id = element.father.dom.hiddenInput[row].name;

}

globals.enter = function(ipiresia) {
	var komotis = globals.curTarget.kom;
	if(komotis.currentRow >= komotis.dom.perigrafiInput.length) {
		alert("End of column.");
	}
	else{
		if(komotis.dom.perigrafiInput[komotis.currentRow].value == ""){
			var perigrafiInput = komotis.dom.perigrafiInput[komotis.currentRow];
			var posoInput = komotis.dom.posoInput[komotis.currentRow];
			var hiddenInput = komotis.dom.hiddenInput[komotis.currentRow];
			perigrafiInput.value = ipiresia.father.perigrafi;
			posoInput.value = ipiresia.father.timi;
			hiddenInput.value = komotis.id + "," + komotis.currentRow + "," + ipiresia.father.id + "," + ipiresia.father.timi;
			//hiddenInput.id += "_" + ipiresia.father.id;
			//hiddenInput.id += "_" + ipiresia.father.timi;
			
			//hiddenInput.name += "_" + ipiresia.father.id;
			//hiddenInput.name += "_" + ipiresia.father.timi;

			console.log("(globals.enter) - komotis.onoma: " + komotis.onoma + ". ipiresiaTitlos = " + ipiresia.father.perigrafi);
			komotis.currentRow++;
			if(komotis.currentRow < komotis.dom.perigrafiInput.length) {
				globals.setCurInput(komotis.dom.perigrafiInput[komotis.currentRow]);
			}
			else{
				komotis.currentRow--;
			}
		}
		else{
			komotis.currentRow++;
			globals.enter(ipiresia);
		}
	}

}

globals.del = function(elem) {
	elem.value = "";
	elem.father.dom.posoInput[elem.aa].value = "";
	elem.father.dom.hiddenInput[elem.aa].value = "";
	//elem.father.dom.hiddenInput[elem.aa].id = "data_" + elem.father.id + "_" + elem.aa;
	//elem.father.dom.hiddenInput[elem.aa].name = "data_" + elem.father.id + "_" + elem.aa;
	elem.focus();
}

globals.setCurInput = function(element) {
	globals.curTarget.kom = element.father;
	globals.curTarget.kom.currentRow = parseInt(element.aa);
	console.log("kom = " + element.father.onoma + " . sira = " + element.father.sira + " row = " + element.father.currentRow + " .");
}

globals.setCurKom = function(element) {
	globals.curTarget.kom = element.father;
	globals.setCurInput(globals.curTarget.kom.dom.perigrafiInput[1]);
}

globals.send = function(kom) {
	for(var x = 0; x < kom; x++) {
		console.log(komotes[x].onoma);
		for(var y = 1; y <= 21; y++) {
			console.log(komotes[x].dom.perigrafiInput[y].value);

			komotes[x].dom.perigrafiInput[y].setAttribute("disabled", "disabled");
			komotes[x].dom.posoInput[y].setAttribute("disabled", "disabled");
		}
	}
	//alert();
}
